<#include "../common/header.htm">
<div class="content-wrapper">
	<section class="content-header">
		<ol class="breadcrumb">
			<li><a href="${ctx}/">首页</a></li>
			<li class="active"><a href="${ctx}/userorder/list">订单管理</a></li>
		</ol>
	</section>
	<section class="content">
		<div class="box">
			<form action="${ctx}/userorder/list" method="get" id="listForm">
				<div class="box-header">
					<div class="row RowSearchWork">
						<div class="col-sm-12">
						    <div class="col-sm-2">
								<label>订单类型：</label> 
								<select class="form-control" name="orderType">
								   <option value="">请选择</option>
								   <#list cardUseMap as key, value>
										<#if orderType?? && key == orderType?c>
											<option value="${key}" selected="selected">${value}</option>
										<#else>
											<option value="${key}">${value}</option>
										</#if> 
								   </#list>
								 </select>
							</div>
							<div class="col-sm-2 ">
								<label>订单号：</label>
								<input type="text" class="form-control" placeholder="订单号" name="orderSn" value="${orderSn!}"  />
							</div>
							<div class="col-sm-1 ml60">
								<input type="button"  class="btn btn-default btn-search" onclick="userorder.query()" value="查询" />
							</div>
						</div>
					</div>
				</div>
				
				<div class="box-body">
					<table class="table table-hover table-bordered table-striped table-hide">
						<thead>
							<tr>
								<th width="4%">序号</th>
								<th width="18%">订单号</th>
								<th width="8%">公司名称</th>
								<th width="6%">类型</th>
								<th width="12%">创建时间</th>
								<th width="8%">支付金额(元)</th>
								<th width="8%">状态</th>
								<th width="8%">操作</th>
							</tr>
						</thead>
						<tbody>
							<#list page.content as p>
								<tr>
									<td>${page.size * page.number + p_index+1}</td>
									<td>${p.orderSn!}</td>
									<td>${p.companyName!}</td>
									<td><@mymacro.mapGet cardUseMap p.orderType></@mymacro.mapGet></td>
									<td><@mymacro.dateFormat p.createTime "yyyy-MM-dd HH:mm:ss"></@mymacro.dateFormat></td>
									<td>${p.payAmount!}</td>
									<td><@mymacro.mapGet orderStateMap p.orderState></@mymacro.mapGet></td>
									<td>
										<#if p.orderState == 1>
											<a href="javascript:void(0)" onclick="userorder.toGiveCard('${p.companyId}')">赠送卡券</a>
											<a href="javascript:void(0)" onclick="userorder.closeCard('${p.orderId}')">关闭</a>
										<#elseif p.orderState == 2>
											<#if !p.workFormId??>
												<a href="javascript:void(0)" onclick="userorder.toWorkForm('${p.orderId}')">新增工单</a>
												<a href="javascript:void(0)" onclick="userorder.relatedWorkForm('${p.orderId}')">关联工单</a>
											<#else>
												<a href="javascript:void(0)" onclick="userorder.viewWorkForm('${p.workFormNo}')">查看工单</a>
											</#if>
										<#elseif p.orderState == 3>
											<#if p.workFormId??>
												<a href="javascript:void(0)" onclick="userorder.viewWorkForm('${p.workFormNo}')">查看工单</a>
											<#else>
												--
											</#if>
										<#else>
											--
										</#if>
									</td>
								</tr>
							</#list>
						</tbody>
					</table>
					<#include "../common/page.htm">
				</div>
			</form>
		</div>
	</section>
</div>
<#include "../common/footer.htm">

<script type="text/javascript">
// 	$(function () {
// 		$('#searchStartTime').datepicker({
// 			autoclose: true,
// 			format: 'yyyy-mm-dd',
// 			clearBtn: true,
// 			language: 'zh-CN'
// 		});
// 		$('#searchEndTime').datepicker({
// 			autoclose: true,
// 			clearBtn: true,
// 			format: 'yyyy-mm-dd',
// 			language: 'zh-CN'
// 		});
// 	});
	
	var is_submit=false;
	var userorder = {
		query : function() {
			// 查询    
			$(":input[name='pageNumber']").val(0);
			$("form").submit();
		},
		toWorkForm : function (orderId) {
			// 跳到新增工单 
			// console.log('orderId='+orderId);
			window.location.href= '${ctx}/workform/form?orderId='+orderId;
		},
		relatedWorkForm : function (orderId) {
			// 关联工单 (弹一个输入框，输入工单ID，点确定关联工单)
			// console.log('orderId='+orderId);
			if(is_submit){
				return;
			}
			is_submit=true;
			
			orderId = orderId ? orderId : "";
			var title = "关联工单";
			
			var j = 0;
			base.getView({
				url:"${ctx}/userorder/relatedWork?orderId="+orderId,
				success:function(result){
					// console.log(result);
					bootbox.dialog({
						title:title,
						message:result,
						size:"large",
						closeButton : false,
						buttons:{
							cancel:{
								label:"取消",
								className:"btn btn-secondary",
								callback:function(){
									is_submit=false;
								}
							},
							sure:{
								label:"确定",
								className:"btn btn-primary",
								callback:function() {
									if (j != 0) {
										is_submit=false;
										return ;
									}
									j++;
									base.validAndAjaxSubmitJson($("#relatedWorkForm"), function () {
										j = 0;
									}, function (data) {
										if (data.result == 'success') {
											$("#listForm").submit();
										} else {
											j = 0;
										}
									});
									return false;
								}
							}
						}
					})
				}
			});
		},
		viewWorkForm : function (workFormNo) {
			// 查看工单 (弹一个输入框，显示工单编号)
			bootbox.alert("关联的工单编号: " + workFormNo);
		},
		closeCard : function (orderId) {
			// TODO 关闭订单  
			// console.log('orderId='+orderId);
			if(is_submit){
				return;
			}
			is_submit=true;
			
			orderId = orderId ? orderId : "";
			var title = "关闭订单";
			
			var j = 0;
			base.getView({
				url:"${ctx}/userorder/close?orderId="+orderId,
				success:function(result){
					// console.log(result);
					bootbox.dialog({
						title:title,
						message:result,
						size:"large",
						closeButton : false,
						buttons:{
							cancel:{
								label:"取消",
								className:"btn btn-secondary",
								callback:function(){
									is_submit=false;
								}
							},
							sure:{
								label:"确定",
								className:"btn btn-primary",
								callback:function() {
									if (j != 0) {
										is_submit=false;
										return ;
									}
									j++;
									base.validAndAjaxSubmitJson($("#closeForm"), function () {
										j = 0;
									}, function (data) {
										if (data.result == 'success') {
											$("#listForm").submit();
										} else {
											j = 0;
										}
									});
									return false;
								}
							}
						}
					})
				}
			});
		},
		toGiveCard : function (companyId) {
			// 赠送卡券   
			if(is_submit){
				return;
			}
			is_submit=true;
			
			companyId = companyId ? companyId : "";
			var title = "赠送卡券";
			
			var j = 0;
			base.getView({
				url:"${ctx}/userorder/toGiveCard",
				success:function(result){
					// console.log(result);
					bootbox.dialog({
						title:title,
						message:result,
						size:"large",
						closeButton : false,
						buttons:{
							cancel:{
								label:"取消",
								className:"btn btn-secondary",
								callback:function(){
									is_submit=false;
								}
							},
							sure:{
								label:"确定",
								className:"btn btn-primary",
								callback:function() {
									if (j != 0) {
										is_submit=false;
										return ;
									}
									j++;
									var params = getQueryParams();
									params["companyId"] = companyId;
									console.log(params);
									var giveResult = false;
									$.ajax({
										url : "${ctx}/userorder/doGiveCard",
										type : 'post',
										dataType : 'json',
										async : false,
										data : params,
										success : function(response) {
											if (response.result == 'success') {
												giveResult = true;
											}
										}
									});
									if (giveResult) {
										$("#listForm").submit();
									} else {
										j = 0;
									}
									return false;
								}
							}
						}
					})
				}
			});
		}
	}
</script>